﻿using System;
using System.Diagnostics;

namespace SfsTools.Core
{
    /// <summary>
    /// Factory method for <see cref="IndexEntry"/> objects.
    /// </summary>
    internal class IndexEntryFactory
    {
        #region Methods

        /// <summary>
        /// Creates an <see cref="IndexEntry"/> from the specified buffer.
        /// </summary>
        /// <param name="position">The position of the entry on the volume.</param>
        /// <param name="buffer">The buffer.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">The argument <paramref name="buffer"/> is <see langword="null"/>.</exception>
        [DebuggerStepThrough]
        public IndexEntry GetEntryFor(Int64 position, byte[] buffer)
        {
            if (buffer == null)
                throw new ArgumentNullException("buffer");
            switch (buffer[0])
            {
                case 0:
                    {
                        return new UnknownIndexEntry(position, buffer);
                    }
                case 1:
                    {
                        return new VolumeIdentifierEntry(position, buffer);
                    }
                case 2:
                    {
                        return new StartingMarkerEntry(position, buffer);
                    }
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                    {
                        return new UnknownIndexEntry(position, buffer);
                    }
                case 16:
                    {
                        return new UnusedIndexEntry(position, buffer);
                    }
                case 17:
                    {
                        return new PresentDirectoryEntry(position, buffer);
                    }
                case 18:
                    {
                        return new PresentFileEntry(position, buffer);
                    }
                case 19:
                case 20:
                case 21:
                case 22:
                case 23:
                    {
                        return new UnknownIndexEntry(position, buffer);
                    }
                case 24:
                    {
                        return new BadSectorEntry(position, buffer);
                    }
                case 25:
                    {
                        return new DeletedDirectoryEntry(position, buffer);
                    }
                case 26:
                    {
                        return new DeletedFileEntry(position, buffer);
                    }
                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                    {
                        return new UnknownIndexEntry(position, buffer);
                    }
                default:
                    {
                        return new ContinuationEntry(position, buffer);
                    }
            }
        }

        #endregion

        #region Constructors
        /// <overloads>Initializes a new instance of <see cref="IndexEntryFactory"/>.</overloads>
        /// <summary>
        /// Initializes a new instance of <see cref="IndexEntryFactory"/>.
        /// </summary>
        [DebuggerStepThrough]
        public IndexEntryFactory()
            : base()
        {
        }
        #endregion
    }
}
